package spring.cloud.tasks.executor.task.statistics;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.listener.analyse.AnalyseService;
import spring.cloud.tasks.executor.listener.config.TaskConfiguration;
import spring.cloud.tasks.executor.listener.task_execute.TaskExecutorInfoService;
import spring.cloud.tasks.executor.task.TaskExecutor;

/**
 * 统计处理数据数量的作业.
 */
@Slf4j
@Setter
@Getter
public class ProcessCountTask implements Runnable {

    private final TaskConfiguration taskConfiguration;

    private final TaskExecutorInfoService taskExecutorInfoService;

    private final AnalyseService analyseService;

    public ProcessCountTask(final TaskExecutor taskConfiguration) {
        this.taskConfiguration = taskConfiguration.getTaskConfiguration();
        taskExecutorInfoService = taskConfiguration.getTaskExecutorInfoService();
        analyseService = taskConfiguration.getAnalyseService();
    }

    @Override
    public void run() {
        String taskId = taskConfiguration.getTaskId();
        taskExecutorInfoService.persistProcessSuccessCount(ProcessCountStatistics.getProcessSuccessCount(taskId, taskExecutorInfoService.getExecutorId()));
        taskExecutorInfoService.persistProcessFailureCount(ProcessCountStatistics.getProcessFailureCount(taskId, taskExecutorInfoService.getExecutorId()));
        analyseService.persistTotalCountDelta();
        analyseService.persistFailureCountDelta();
    }
}
